package com.lwj.hiring.talent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lwj.hiring.common.util.AppUtil;
import com.lwj.hiring.company.dao.ApplicationDao;
import com.lwj.hiring.company.entity.ApplicationEntity;
import com.lwj.hiring.talent.dao.ResumeDao;
import com.lwj.hiring.talent.dao.TalentDao;
import com.lwj.hiring.talent.entity.ResumeEntity;
import com.lwj.hiring.talent.entity.Talent;

/**
 * Talent Servlet
 * 
 * @author Administrator
 *
 */
public class TalentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static TalentDao talentDao = new TalentDao();
	private static ResumeDao resumeDao = new ResumeDao();
	private static ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("login".equals(command)) {
			login(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("queryById".equals(command)) {
			queryById(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		}
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.getSession().invalidate();
		request.getRequestDispatcher("PositionServlet?command=queryAll")
				.forward(request, response);
	}

	/**
	 * 修改求职者信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbys = request.getParameterValues("hobby");
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < hobbys.length; i++) {
			if (i == hobbys.length - 1) {
				sb.append(hobbys[i]);
			} else {
				sb.append(hobbys[i] + "|");
			}
		}
		List<Object> list = new ArrayList<>();
		list.add(name);
		list.add(phone);
		list.add(email);
		list.add(age);
		list.add(gender);
		list.add(sb.toString());
		list.add(id);
		int row = talentDao.update(list);
		if (row > 0) {
			request.setAttribute("message", "修改信息成功！");
			request.getRequestDispatcher("view/talent/main.jsp").forward(
					request, response);
		}
	}

	/**
	 * 根据Id查询用户信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void queryById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Talent talent = talentDao.queryById(id);
		request.setAttribute("talent", talent);
		request.getRequestDispatcher("view/talent/talent-info.jsp").forward(
				request, response);
	}

	/**
	 * 求职者注册
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		List<Object> list = new ArrayList<>();
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		String[] hobbys = request.getParameterValues("hobby");
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < hobbys.length; i++) {
			if (i == hobbys.length - 1) {
				sb.append(hobbys[i]);
			} else {
				sb.append(hobbys[i] + "|");
			}
		}
		list.add(code);
		list.add(password);
		list.add(name);
		list.add(phone);
		list.add(email);
		list.add(age);
		list.add(gender);
		list.add(sb.toString());

		int row = talentDao.add(list);
		if (row > 0) {
			request.setAttribute("message", "注册成功，请登录！");
			response.sendRedirect("view/talent/talent-login.jsp");
		} else {
			request.setAttribute("message", "注册失败！");
			request.getRequestDispatcher("view/talent/talent-register.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 求职者登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String code = request.getParameter("code");
		String password = request.getParameter("password");
		Talent talent = talentDao.login(code, password);
		if (talent != null) {
			request.getSession().invalidate();
			request.getSession().setAttribute("talent", talent);
			response.sendRedirect("TalentServlet?command=main");
		} else {

			request.setAttribute("message", "帐号或密码错误，请重新输入！");
			request.setAttribute("code", code);
			request.getRequestDispatcher("view/talent/talent-login.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 到主页时的查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String rowStr = request.getParameter("row");
		// 非登录操作
		if (rowStr != null) {
			int row = Integer.parseInt(rowStr);
			if (row == 1) {
				request.setAttribute("success", "操作成功！");
			} else {
				request.setAttribute("error", "操作失败！");
			}
		}
		int id = AppUtil.getTalentId(request, response);
		// 简历列表
		List<ResumeEntity> resumeList = resumeDao.list(id);
		request.setAttribute("resumeList", resumeList);
		// 求职列表
		List<ApplicationEntity> applicationList = applicationDao.list(id);
		request.setAttribute("applicationList", applicationList);
		request.getRequestDispatcher("view/talent/main.jsp").forward(request,
				response);
	}
}
